﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text.RegularExpressions;

namespace FieldPDFTools
{
    /// <summary>
    /// 配置类
    /// </summary>
    public class ConfigHelper
    {
        /// <summary>
        /// 读取所有配置
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> ReadAllSettings()
        {
            Dictionary<string, string> directory = new();
            try
            {
                var appSettings = ConfigurationManager.AppSettings;

                if (appSettings.Count == 0)
                {
                    Console.WriteLine("AppSettings is empty.");
                }
                else
                {
                    foreach (var key in appSettings.AllKeys)
                    {
                        //Console.WriteLine("Key: {0} Value: {1}", key, appSettings[key]);
                        directory.Add(key, appSettings[key]);
                    }
                }
            }
            catch (ConfigurationErrorsException)
            {
                Console.WriteLine("Error reading app settings");
            }
            return directory;
        }

        /// <summary>
        /// 读取指定配置
        /// </summary>
        /// <param name="key"></param>
        /// <param name="isEscape">是否需要转义</param>
        /// <returns></returns>
        public static string ReadSetting(string key, bool isEscape = false)
        {
            try
            {
                var appSettings = ConfigurationManager.AppSettings;
                string result = appSettings[key] ?? string.Empty;
                return isEscape ? Regex.Escape(result) : result;
            }
            catch (ConfigurationErrorsException)
            {
                Console.WriteLine("Error reading app settings");
            }
            return string.Empty;
        }

        /// <summary>
        /// 读取指定配置，不存在时设置默认值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <param name="isEscape">是否需要转义</param>
        /// <returns></returns>
        public static string ReadOrSetDefaultSetting(string key, string defaultValue, bool isEscape = false)
        {
            if (string.IsNullOrEmpty(ReadSetting(key, isEscape)))
            {
                AddUpdateAppSettings(key, defaultValue);
                return defaultValue;
            }
            return ReadSetting(key, isEscape);
        }

        /// <summary>
        /// 添加或更新配置
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void AddUpdateAppSettings(string key, string value)
        {
            try
            {
                var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                var settings = configFile.AppSettings.Settings;
                if (settings[key] == null)
                {
                    settings.Add(key, value);
                }
                else
                {
                    settings[key].Value = value;
                }
                configFile.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
            }
            catch (ConfigurationErrorsException)
            {
                Console.WriteLine("Error writing app settings");
            }
        }

    }
}
